var resourceUrl = getApp().request("imgUrl");
const app = getApp();
var config = require('../../utils/parts.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    resourceUrl:resourceUrl
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
    
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
    
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    
  },

  getUserInfo(re){
    console.log(re);
    var nickName = re.detail.userInfo.nickName;
    var avatarUrl = re.detail.userInfo.avatarUrl;
    var encryptedData = re.detail.encryptedData;
    var iv = re.detail.iv;
    app.globalData.nickName = nickName;
    app.globalData.avatarUrl = avatarUrl;
    wx.setStorageSync("nickName", nickName);
    wx.setStorageSync("avatarUrl", avatarUrl);
    wx.login({
      success: res => {
        if (res.code) {
          console.log("第一次登录");
          wx.request({
            url: config.authUrl,
            method: 'POST',
            header: {
              'content-type': 'application/x-www-form-urlencoded'
            },
            data: {
              code: res.code,
              encryptedData: encryptedData,
              iv: iv
            },
            success: function (res) {
              console.log("第一次获取标识");
              console.log(res);
              if (res.statusCode != 200) {
                getApp().showModal("服务器异常");
                return
              }
              if (res.data.code == 200) {
                //res.data.data里是用户唯一标识
                var salt = res.data.data.salt;
                var userId = res.data.data.userId;
                var arr = [userId, salt];
                wx.setStorageSync("session_key", arr);
                app.globalData.session_key.userId = userId;
                app.globalData.session_key.salt = salt;
                wx.navigateBack({
                  delta: 1
                });
              } else {
                wx.showLoading({
                  title: '页面溜走了!',
                  mask: true
                })
              }
            },
            fail: function (res) {
              console.log("没有session时登录后fail");
              console.log(res);
              getApp().showModal("网络奔溃啦，请关闭后重试哦！");
            }
          })
        } else {
          console.log('登录失败！' + res.errMsg);
        }
      }
    })
  }
})